<?php
/**
 * Created by PhpStorm.
 * User: z-benjamin
 * Date: 16/9/9
 * Time: 上午11:18
 */

namespace lite\controllers;

use weixin\models\game\AR\GameAR;
use weixin\models\game\Game;
use weixin\models\user\User;
use admin\models\property\Property;
use lite\models\Weixin;
use Yii;
use weixin\libs\CommonController;


class GameController extends CommonController {

    private $property_scan_key = 'property_scan_key';

    public function actionGet_game() {

        $game_id = yii::$app->request->post('game_id');
        $accessToken = yii::$app->request->post('access_token');
        $user = Weixin::checkIsLogin($accessToken);
        if ($user === false) {
            return $this->ajaxResponseError('用户还未登录，请重新登录', -999);
        }

        $game = new Game();
        $gameInfo = $game->getGame($game_id);
        $playerInfo = $game->getPlayer($game_id, $user);
        if(false===$playerInfo){
            return $this->ajaxResponseError($game->getErrMsg(), -1);
        }
        $role = $game->getRole($playerInfo->role_id);
        if($playerInfo && $gameInfo) {
            $votes = $game->getUserVote($playerInfo->id);
            return $this->ajaxResponseSuccess(['player' => $playerInfo, 'game' => $gameInfo, 'votes' => $votes,
                                               'role' => $role]);
        } else {
            return $this->ajaxResponseError($game->getErrMsg(), -1);
        }
    }

    public function actionGet_game_status() {

        $game_id = yii::$app->request->post('game_id');
        $accessToken = yii::$app->request->post('access_token');
        $user = Weixin::checkIsLogin($accessToken);
        if ($user === false) {
            return $this->ajaxResponseError('用户还未登录，请重新登录', -999);
        }

        $game=GameAR::find()->select('status')->andWhere(['id'=>$game_id])->one();
        if($game) {
            return $this->ajaxResponseSuccess($game);
        } else {
            return $this->ajaxResponseError('游戏状态异常', -1);
        }

    }

    public function actionGet_game_players() {
        $accessToken = yii::$app->request->post('access_token');
        $user = Weixin::checkIsLogin($accessToken);
        if ($user === false) {
            return $this->ajaxResponseError('用户还未登录，请重新登录', -999);
        }
        $game_id = yii::$app->request->post('game_id');

        $game = new Game();
        $players = $game->getPlayers($game_id);
        if($players) {
            return $this->ajaxResponseSuccess($players);
        } else {
            return $this->ajaxResponseError($game->getErrMsg(), -1);
        }
    }


    public function actionGet_games() {
        $accessToken = yii::$app->request->post('access_token');
        $user = Weixin::checkIsLogin($accessToken);
        if ($user === false) {
		return $this->ajaxResponseError('用户还未登录，请重新登录', -999);
        }
	
        $story_id = yii::$app->request->post('story_id');
        $status = yii::$app->request->post('status');
        $offset = yii::$app->request->post('offset');
        $limit = yii::$app->request->post('limit');


        $game = new Game();
        $result = $game->getGames($story_id, $status,$user->id, $offset, $limit);
        if($result) {
            return $this->ajaxResponseSuccess($result);
        } else {
            return $this->ajaxResponseError($game->getErrMsg(), -1);
        }

    }

    public function actionEnd_game() {
        $user_id = yii::$app->session->get('user_id');
        $game_id = yii::$app->request->post('game_id');
        if(!$user_id) {
            return $this->ajaxResponseNotLogin();
        }

        $game = new Game();
        $result = $game->endGame($user_id, $game_id);
        if($result) {
            return $this->ajaxResponseSuccess($result);
        } else {
            return $this->ajaxResponseError($game->getErrMsg(), -1);
        }
    }

    public function actionUpload_proof() {
        $user_id = yii::$app->session->get('user_id');
        $game_id = yii::$app->request->post('game_id');
        $filename = yii::$app->request->post('filename');
        if(!$user_id) {
            return $this->ajaxResponseNotLogin();
        }

        $game = new Game();
        $result = $game->uploadProof($game_id, $user_id, $filename);
        if($result) {
            return $this->ajaxResponseSuccess($result);
        } else {
            return $this->ajaxResponseError($game->getErrMsg(), -1);
        }
    }

    public function actionDel_proof() {
        $accessToken = yii::$app->request->post('access_token');
        $user = Weixin::checkIsLogin($accessToken);
        if ($user === false) {
            return $this->ajaxResponseError('用户还未登录，请重新登录', -999);
        }

        $proof_id = yii::$app->request->post('proof_id');


        $game = new Game();
        $result = $game->deleteProof($user->id, $proof_id);
        if($result) {
            return $this->ajaxResponseSuccess('提交成功！');
        } else {
            return $this->ajaxResponseError($game->getErrMsg(), -1);
        }
    }

    public function actionGet_proofs() {
        $accessToken = yii::$app->request->post('access_token');
        $user = Weixin::checkIsLogin($accessToken);
        if ($user === false) {
            return $this->ajaxResponseError('用户还未登录，请重新登录', -999);
        }

        $player_id = yii::$app->request->post('player_id');
        $game_id = yii::$app->request->post('game_id');

        if(!$game_id) {
            return $this->ajaxResponseError('游戏参数错误', -1);
        }

        $game = new Game();
        $result = $game->getProofs($game_id, $user->id, 0, 10, 0,$player_id);
        if($result) {
            return $this->ajaxResponseSuccess($result);
        } else {
            return $this->ajaxResponseError($game->getErrMsg(), -1);
        }
    }

    public function actionVote() {
        $accessToken = yii::$app->request->post('access_token');
        $user = Weixin::checkIsLogin($accessToken);
        if ($user === false) {
            return $this->ajaxResponseError('用户还未登录，请重新登录', -999);
        }

        $game_id = yii::$app->request->post('game_id');
        $to_player = yii::$app->request->post('to_player');


        if(!$game_id) {
            return $this->ajaxResponseError('投票参数错误，请重新投票', -1);
        }
        if(!$to_player) {
            return $this->ajaxResponseError('投票参数错误，请重新投票', -1);
        }


        $game = new Game();
        $result = $game->vote($user->id, $to_player, $game_id);
        if($result) {
            return $this->ajaxResponseSuccess($result);
        } else {
            return $this->ajaxResponseError($game->getErrMsg(), -1);
        }
    }

    public function actionVote_sum() {
        $user_id = yii::$app->session->get('user_id');
        $game_id = yii::$app->request->post('game_id');
        if(!$user_id) {
            return $this->ajaxResponseNotLogin();
        }
        if(!$game_id) {
            return $this->ajaxResponseError('投票参数错误，请重新投票', -1);
        }
        $game = new Game();
        $result = $game->voteSum($user_id, $game_id);
        if($result) {
            return $this->ajaxResponseSuccess($result);
        } else {
            return $this->ajaxResponseError($game->getErrMsg(), -1);
        }
    }

    public function actionVote_result() {
        $accessToken = yii::$app->request->post('access_token');
        $user = Weixin::checkIsLogin($accessToken);
        if ($user === false) {
            return $this->ajaxResponseError('用户还未登录，请重新登录', -999);
        }

        $game_id = yii::$app->request->post('game_id');

        if(!$game_id) {
            return $this->ajaxResponseError('投票参数错误，请重新投票', -1);
        }
        $game = new Game();
        $result = $game->voteResult($user->id, $game_id);
        if($result) {
            return $this->ajaxResponseSuccess($result);
        } else {
            return $this->ajaxResponseError($game->getErrMsg(), -1);
        }
    }

    public function actionGet_game_info() {
        $user_id = yii::$app->session->get('user_id');
        $game_id = yii::$app->request->post('game_id');
        if(!$user_id) {
            return $this->ajaxResponseNotLogin();
        }
        if(!$game_id) {
            return $this->ajaxResponseError('游戏参数错误，请重试', -1);
        }
        $game = new Game();
        $result = $game->getGameInfo($user_id, $game_id);
        if($result) {
            return $this->ajaxResponseSuccess($result);
        } else {
            return $this->ajaxResponseError($game->getErrMsg(), -1);
        }
    }

    public function actionGet_player_info() {
        $user_id = yii::$app->session->get('user_id');
        $player_id = yii::$app->request->post('player_id');
        if(!$user_id) {
            return $this->ajaxResponseNotLogin();
        }
        if(!$player_id) {
            return $this->ajaxResponseError('游戏参数错误，请重试', -1);
        }
        $game = new Game();
        $result = $game->getPlayerInfo($user_id, $player_id);
        if($result) {
            return $this->ajaxResponseSuccess($result);
        } else {
            return $this->ajaxResponseError($game->getErrMsg(), -1);
        }
    }

    public function actionFirst_time_vote() {
        $user_id = yii::$app->session->get('user_id');
        $game_id = yii::$app->request->post('game_id');

        if(!$user_id) {
            return $this->ajaxResponseNotLogin();
        }
        if(!$game_id) {
            return $this->ajaxResponseError('游戏参数错误', -1);
        }
        $game = new Game();
        $result = $game->firstTimeVote($user_id, $game_id);
        if($result) {
            return $this->ajaxResponseSuccess('设置成功');
        } else {
            return $this->ajaxResponseError($game->getErrMsg(), -1);
        }
    }

    public function actionSecond_time_vote() {
        $user_id = yii::$app->session->get('user_id');
        $game_id = yii::$app->request->post('game_id');

        if(!$user_id) {
            return $this->ajaxResponseNotLogin();
        }
        if(!$game_id) {
            return $this->ajaxResponseError('游戏参数错误', -1);
        }
        $game = new Game();
        $result = $game->secondTimeVote($user_id, $game_id);
        if($result) {
            return $this->ajaxResponseSuccess('设置成功');
        } else {
            return $this->ajaxResponseError($game->getErrMsg(), -1);
        }
    }

    public function actionUpload_weixin_images(){
    }

    public function actionGet_game_hide_status(){
        $game_id = yii::$app->request->post('game_id');
        $result=GameAR::find()->select('hide_status')->where(['id'=>$game_id])->one();
        if($result) {
            return $this->ajaxResponseSuccess($result);
        } else {
            return $this->ajaxResponseError('查询失败', -1);
        }
    }

    public function actionChange_game_hide_status(){
        $user_id = yii::$app->session->get('user_id');
        $game_id = yii::$app->request->post('game_id');

        if(!$user_id) {
            return $this->ajaxResponseNotLogin();
        }

        $userAR = new User();
        $user = $userAR->getUser($user_id);
        if(!$user||$user->is_admin==0) {
            return $this->ajaxResponseNotLogin();
        }
        $game=new Game();
        $result=$game->changeHideStatus($game_id);
        if($result) {
            return $this->ajaxResponseSuccess();
        } else {
            return $this->ajaxResponseError('修改失败', -1);
        }

    }

    public function actionProperty_detail(){
        $property_id = yii::$app->request->post('property_id');
        $game_id = yii::$app->request->post('game_id');
        $cache = Yii::$app->cache;
        $property_scan = $cache->get($this->property_scan_key.'_'.$game_id);
        if(!$property_scan){
            $property=new Property();
            $result=$property->getPropertyDetail($property_id);
            if($result){
                $cache->set($this->property_scan_key.'_'.$game_id,1,120);
                return $this->ajaxResponseSuccess($result);
            }else{
                return $this->ajaxResponseError($property->getErrMsg(),-1);
            }
        }else{
            return $this->ajaxResponseError('技能每2分钟开启一次，请稍后再试',-1);
        }
    }
}
